// +----------------------------------------------------------------------
// | JavaWeb敏捷开发框架 [ 赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2023 上海JavaWeb研发中心
// +----------------------------------------------------------------------
// | Licensed LGPL-3.0 JavaWeb并不是自由软件，未经许可禁止去掉相关版权
// +----------------------------------------------------------------------
// | 官方网站: https://www.javaweb.vip
// +----------------------------------------------------------------------
// | Author: @鲲鹏 团队荣誉出品
// +----------------------------------------------------------------------
// | 版权和免责声明:
// | 本团队对该软件框架产品拥有知识产权（包括但不限于商标权、专利权、著作权、商业秘密等）
// | 均受到相关法律法规的保护，任何个人、组织和单位不得在未经本团队书面授权的情况下对所授权
// | 软件框架产品本身申请相关的知识产权，禁止用于任何违法、侵害他人合法权益等恶意的行为，禁
// | 止用于任何违反我国法律法规的一切项目研发，任何个人、组织和单位用于项目研发而产生的任何
// | 意外、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、
// | 附带或衍生的损失等)，本团队不承担任何法律责任，本软件框架禁止任何单位和个人、组织用于
// | 任何违法、侵害他人合法利益等恶意的行为，如有发现违规、违法的犯罪行为，本团队将无条件配
// | 合公安机关调查取证同时保留一切以法律手段起诉的权利，本软件框架只能用于公司和个人内部的
// | 法律所允许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

package ${packageName}.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.utils.CommonUtils;
import com.javaweb.common.common.BaseQuery;
import com.javaweb.common.common.BaseServiceImpl;
import ${packageName}.constant.${entityName}Constant;
import ${packageName}.entity.${entityName};
import ${packageName}.mapper.${entityName}Mapper;
import ${packageName}.query.${entityName}Query;
import ${packageName}.service.I${entityName}Service;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.system.utils.ShiroUtils;
import ${packageName}.vo.${entityName?lower_case}.${entityName}InfoVo;
import ${packageName}.vo.${entityName?lower_case}.${entityName}ListVo;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

/**
  * <p>
  * ${tableAnnotation} 服务类实现
  * </p>
  *
  * @author ${author}
  * @since ${date}
  */
@Service
public class ${entityName}ServiceImpl extends BaseServiceImpl<${entityName}Mapper, ${entityName}> implements I${entityName}Service {

    @Autowired
    private ${entityName}Mapper ${entityName?uncap_first}Mapper;

    /**
     * 获取数据列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    public JsonResult getList(BaseQuery query) {
        ${entityName}Query ${entityName?uncap_first}Query = (${entityName}Query) query;
        // 查询条件
        QueryWrapper<${entityName}> queryWrapper = new QueryWrapper<>();
<#if model_column?exists>
    <#list model_column as model>
        <#if model.columnName = 'name' || model.columnName = 'title'>
        // ${model.columnComment!}
        if (!StringUtils.isEmpty(${entityName?uncap_first}Query.get${model.columnName?cap_first}())) {
            queryWrapper.like("${model.columnName}", ${entityName?uncap_first}Query.get${model.columnName?cap_first}());
        }
        </#if>
        <#if model.hasColumnCommentValue = true>
        // ${model.columnComment!}
        if (!StringUtils.isEmpty(${entityName?uncap_first}Query.get${model.columnName?cap_first}())) {
            queryWrapper.eq("${model.columnName2}", ${entityName?uncap_first}Query.get${model.columnName?cap_first}());
        }
        </#if>
    </#list>
</#if>
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByDesc("id");

        // 获取数据列表
        IPage<${entityName}> page = new Page<>(${entityName?uncap_first}Query.getPage(), ${entityName?uncap_first}Query.getLimit());
        IPage<${entityName}> pageData = ${entityName?uncap_first}Mapper.selectPage(page, queryWrapper);
        pageData.convert(x -> {
            ${entityName}ListVo ${entityName?uncap_first}ListVo = Convert.convert(${entityName}ListVo.class, x);
<#if model_column?exists>
    <#list model_column as model>
        <#if model.hasColumnCommentValue = true>
            // ${model.columnCommentName}描述
            <#if model.columnNumberValue = true>
            if (${entityName?uncap_first}ListVo.get${model.changeColumnName}() != null && ${entityName?uncap_first}ListVo.get${model.changeColumnName}() > 0) {
                ${entityName?uncap_first}ListVo.set${model.changeColumnName}Name(${entityName}Constant.${entityName?upper_case}_${model.changeColumnName?upper_case}_LIST.get(${entityName?uncap_first}ListVo.get${model.changeColumnName}()));
            }
            <#else>
            if (!StringUtils.isEmpty(${entityName?uncap_first}ListVo.get${model.changeColumnName}())) {
                ${entityName?uncap_first}ListVo.set${model.changeColumnName}Name(${entityName}Constant.${entityName?upper_case}_${model.changeColumnName?upper_case}_LIST.get(${entityName?uncap_first}ListVo.get${model.changeColumnName}()));
            }
            </#if>
        </#if>
        <#if model.columnImage == true>
            // ${model.columnComment}地址
            if (!StringUtils.isEmpty(${entityName?uncap_first}ListVo.get${model.changeColumnName}())) {
                ${entityName?uncap_first}ListVo.set${model.changeColumnName}(CommonUtils.getImageURL(${entityName?uncap_first}ListVo.get${model.changeColumnName}()));
            }
        </#if>
    </#list>
</#if>
            return ${entityName?uncap_first}ListVo;
        });
        return JsonResult.success(pageData);
    }

    /**
     * 获取详情Vo
     *
     * @param id 记录ID
     * @return
     */
    @Override
    public Object getInfo(Serializable id) {
        ${entityName} entity = (${entityName}) super.getInfo(id);
        // 返回视图Vo
        ${entityName}InfoVo ${entityName?uncap_first}InfoVo = new ${entityName}InfoVo();
        // 拷贝属性
        BeanUtils.copyProperties(entity, ${entityName?uncap_first}InfoVo);
<#if model_column?exists>
    <#list model_column as model>
        <#if model.columnImage == true>
        // ${model.columnComment}
        if (!StringUtils.isEmpty(${entityName?uncap_first}InfoVo.get${model.changeColumnName}())) {
            ${entityName?uncap_first}InfoVo.set${model.changeColumnName}(CommonUtils.getImageURL(${entityName?uncap_first}InfoVo.get${model.changeColumnName}()));
        }
        </#if>
    </#list>
</#if>
        return ${entityName?uncap_first}InfoVo;
    }

    /**
     * 添加、更新记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult edit(${entityName} entity) {
<#if model_column?exists>
    <#list model_column as model>
        <#if model.columnImage == true>
        // ${model.columnComment}
        if (entity.get${model.changeColumnName}().contains(CommonConfig.imageURL)) {
            entity.set${model.changeColumnName}(entity.get${model.changeColumnName}().replaceAll(CommonConfig.imageURL, ""));
        }
        </#if>
    </#list>
</#if>
        if (StringUtils.isNotNull(entity.getId()) && entity.getId() > 0) {
    <#if model_column?exists>
        <#list model_column as model>
            <#if model.changeColumnName?uncap_first = 'updateUser'>
            entity.setUpdateUser(ShiroUtils.getUserId());
            </#if>
            <#if model.changeColumnName?uncap_first = 'updateTime'>
            entity.setUpdateTime(DateUtils.now());
            </#if>
        </#list>
    </#if>
        } else {
    <#if model_column?exists>
        <#list model_column as model>
            <#if model.changeColumnName?uncap_first = 'createUser'>
            entity.setCreateUser(ShiroUtils.getUserId());
            </#if>
            <#if model.changeColumnName?uncap_first = 'createTime'>
            entity.setCreateTime(DateUtils.now());
            </#if>
        </#list>
    </#if>
        }
        return super.edit(entity);
    }

    /**
     * 删除记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult delete(${entityName} entity) {
<#if model_column?exists>
    <#list model_column as model>
        <#if model.changeColumnName?uncap_first = 'updateUser'>
        entity.setUpdateUser(1);
        </#if>
        <#if model.changeColumnName?uncap_first = 'updateTime'>
        entity.setUpdateTime(DateUtils.now());
        </#if>
    </#list>
</#if>
        entity.setMark(0);
        return super.delete(entity);
    }

<#if model_column?exists>
    <#list model_column as model>
    <#if model.columnSwitch == true>
    /**
     * 设置${model.columnCommentName}
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult set${model.changeColumnName?cap_first}(${entityName} entity) {
        if (entity.getId() == null || entity.getId() <= 0) {
            return JsonResult.error("记录ID不能为空");
        }
        if (entity.get${model.changeColumnName?cap_first}() == null) {
            return JsonResult.error("记录${model.columnCommentName}不能为空");
        }
        <#if model.columnName == "status">
        return super.set${model.changeColumnName?cap_first}(entity);
        <#else>
        entity.setUpdateUser(ShiroUtils.getUserId());
        entity.setUpdateTime(DateUtils.now());
        boolean result = this.updateById(entity);
        if (!result) {
            return JsonResult.error();
        }
        return JsonResult.success();
        </#if>
    }

    </#if>
    </#list>
</#if>
}